package cn.zymall.common.controller.crud;

import cn.zymall.common.model.BaseEntity;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.AbstractChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.parser.Entity;
import java.lang.annotation.Annotation;

/**
 * @author xuxianda
 * @Description: 公共控制器，继承这个类自动将请求映射到这里的增删查改方法
 * @date 2020.8.15 10:01
 * @Copyright 18h1 g1
 * @return null
 **/
public class BaseController<SERVICE extends IService, ENTITY extends BaseEntity> {

    @Autowired
    SERVICE service;

    /**
     * @param bean
     * @return boolean
     * @desc 新增
     */
    @PostMapping
    public boolean insert(@RequestBody ENTITY bean) {
        System.out.println(bean);
        return service.save(bean);
    }

    /**
     * @param bean
     * @return boolean
     * @desc 根据主键id 修改
     */
    @PutMapping
    public boolean update(@RequestBody ENTITY bean) {
        return service.updateById(bean);
    }

    /**
     * @param id
     * @return boolean
     * @desc 根据主键id 删除
     */
    @DeleteMapping
    public boolean delete(@PathVariable(name = "id") String id) {
        return service.removeById(id);
    }

    /**
     * @param page
     * @param rows
     * @return 分页数据
     * @desc 分页查询，不传参数默认查询1页 10条数据
     */
    @GetMapping
    public Object select(Integer page, Integer rows) {
        if (page == null) {
            page = 1;
        }
        if (rows == null) {
            rows = 10;
        }
        System.out.println(page + "/" + rows);
        Page pages = new Page(page, rows);
        return service.page(pages);
    }

    /**
     * @param id 主键id
     * @return 分页数据
     * @desc 根据主键ID查询一条数据
     */
    @GetMapping("/{id}")
    public Object selectById(@PathVariable(value = "id") Integer id){
        return service.getById(id);
    }

}
